package com.currency.mall.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.currency.mall.R;
import com.currency.mall.event.MainEvent;
import com.currency.mall.ui.login.LoginActivity;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//@Route(path = "/forex/main")
public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    @BindView(R.id.img_my)
    ImageView imgMy;
    @BindView(R.id.txt_my)
    TextView txtMy;
    @BindView(R.id.img_home)
    ImageView imgHome;
    @BindView(R.id.txt_home)
    TextView txtHome;
    @BindView(R.id.ll_home)
    LinearLayout llHome;
    @BindView(R.id.img_optional)
    ImageView imgOptional;
    @BindView(R.id.txt_optional)
    TextView txtOptional;
    @BindView(R.id.ll_optional)
    LinearLayout llOptional;
    @BindView(R.id.img_inquiry)
    ImageView imgInquiry;
    @BindView(R.id.txt_inquiry)
    TextView txtInquiry;
    @BindView(R.id.ll_inquiry)
    LinearLayout llInquiry;
    @BindView(R.id.img_order)
    ImageView imgOrder;
    @BindView(R.id.txt_order)
    TextView txtOrder;
    @BindView(R.id.ll_order)
    LinearLayout llOrder;
    private long exitTime = 0;//物理返回按键的时间差
    private MyFragment mMyFragment;
    private HomeFragment mHomeFragment;
    private OptionalFragment mOptionalFragment;
    private InquiryFragment mInquiryFragment;
    private OrderFragment mOrderFragment;
    private Fragment mCurrentFragment;
    private int mCurrentSelectedIndex; //当前pageIndex

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        StatusBarCompat.setStartBarTranslucent(this);
        StatusBarUtils.StatusBarLightMode(this);
        EventBus.getDefault().register(this);
        mHomeFragment = HomeFragment.newInstance();
        mOptionalFragment = OptionalFragment.newInstance();
        mInquiryFragment = InquiryFragment.newInstance();
        mOrderFragment = OrderFragment.newInstance();
        Log.d("uid:", PreferenceUtils.getUid(getContext()));
        if (savedInstanceState == null) {
            mCurrentFragment = mHomeFragment;
            FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            ft.add(R.id.container, mCurrentFragment).commit();
        }
    }

    @Override
    public void toHome() {
        if (this.mCurrentSelectedIndex != 0) {
            mCurrentSelectedIndex = 0;
            // Selected
            StatusBarCompat.setStartBarTranslucent(this);
            StatusBarUtils.StatusBarLightMode(this);

            imgHome.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home_select));
            txtHome.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_pressed));

            imgOptional.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_optional));
            txtOptional.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgInquiry.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_inquiry));
            txtInquiry.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOrder.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_order));
            txtOrder.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgMy.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_my));
            txtMy.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            this.switchContent(mCurrentFragment, mHomeFragment);
        }
    }

    @Override
    public void toOptional() {
        if (this.mCurrentSelectedIndex != 1) {
            mCurrentSelectedIndex = 1;
            // Selected
            StatusBarUtils.StatusBarLightMode(this);

            imgHome.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home));
            txtHome.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOptional.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_optional_select));
            txtOptional.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_pressed));

            imgInquiry.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_inquiry));
            txtInquiry.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOrder.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_order));
            txtOrder.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgMy.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_my));
            txtMy.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            this.switchContent(mCurrentFragment, mOptionalFragment);
        }
    }

    @Override
    public void toInquiry() {
        if (this.mCurrentSelectedIndex != 2) {
            mCurrentSelectedIndex = 2;
            // Selected
            StatusBarUtils.StatusBarLightMode(this);
            imgHome.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home));
            txtHome.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOptional.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_optional));
            txtOptional.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgInquiry.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_inquiry_select));
            txtInquiry.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_pressed));

            imgOrder.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_order));
            txtOrder.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgMy.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_my));
            txtMy.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            this.switchContent(mCurrentFragment, mInquiryFragment);
        }
    }

    @Override
    public void toOrder() {
        if (this.mCurrentSelectedIndex != 3) {
            mCurrentSelectedIndex = 3;
            // Selected
            StatusBarUtils.StatusBarLightMode(this);
            imgHome.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home));
            txtHome.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOptional.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_optional));
            txtOptional.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgInquiry.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_inquiry));
            txtInquiry.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOrder.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_order_select));
            txtOrder.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_pressed));

            imgMy.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_my));
            txtMy.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

//            mOrderFragment = OrderFragment.newInstance();
            this.switchContent(mCurrentFragment, mOrderFragment);
        }
    }

    @Override
    public void toMyPage() {
        if (this.mCurrentSelectedIndex != 4) {
            mCurrentSelectedIndex = 4;
            // Selected
            StatusBarUtils.StatusBarLightMode(this);

            imgHome.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_home));
            txtHome.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOptional.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_optional));
            txtOptional.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgInquiry.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_inquiry));
            txtInquiry.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgOrder.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_order));
            txtOrder.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_normal));

            imgMy.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.tab_my_select));
            txtMy.setTextColor(ContextCompat.getColor(this, R.color.bottombar_text_pressed));

            mMyFragment = MyFragment.newInstance();
            this.switchContent(mCurrentFragment, mMyFragment);
        }
    }

    private void switchContent(Fragment from, Fragment to) {
        if (mCurrentFragment != to) {
            mCurrentFragment = to;
            FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from).add(R.id.container, to).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MainEvent event) {
        switch (event.action) {
            case MainEvent.HOME_LOGIN:
                toHome();
                toLogin();
                break;
            case MainEvent.TAB_HOME:
                toHome();
                if (mInquiryFragment != null) {
                    mInquiryFragment.reloadInquiry();
                }
                break;
            case MainEvent.LOGINTRELOAD:
                if (mOptionalFragment != null && PreferenceUtils.getLoginStatus()) {
                    mOptionalFragment.reloadOptional();
                }
                if (mInquiryFragment != null) {
                    mInquiryFragment.reloadInquiry();
                }
                if (mOrderFragment != null && PreferenceUtils.getLoginStatus()) {
                    mOrderFragment.reloadOrder(0);
                }
                break;
            case MainEvent.TAB_ORDER:
                toOrder();
                if (mOrderFragment != null) {
                    mOrderFragment.reloadOrder(0);
                }
                break;
            case MainEvent.TAB_INQUIRY:
                toInquiry();
                break;
            case MainEvent.TAB_OPTIONAL:
                toOptional();
                break;
            case MainEvent.TAB_MY:
                    if (mMyFragment != null && PreferenceUtils.getLoginStatus()) {
                        mMyFragment.onRefresh();
                    }
                    break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @OnClick({R.id.ll_home, R.id.ll_optional, R.id.ll_inquiry, R.id.ll_order, R.id.ll_my})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_home:
                toHome();
                break;
            case R.id.ll_optional:
                if (PreferenceUtils.getLoginStatus()) {
                    toOptional();
                } else {
                    toLogin();
                }
                break;
            case R.id.ll_inquiry:
                toInquiry();
                break;
            case R.id.ll_order:
                if (PreferenceUtils.getLoginStatus()) {
                    toOrder();
                    if (mOrderFragment != null) {
                        mOrderFragment.reloadOrder(0);
                    }
                } else {
                    toLogin();
                }
                break;
            case R.id.ll_my:
                if (PreferenceUtils.getLoginStatus()) {
                    toMyPage();
                } else {
                    toLogin();
                }

                break;
        }
    }

    public void toLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
}
